/**
 * 
 */
package com.cuc.platform.core.web.sysadmin.security;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.cuc.platform.core.componet.security.domain.SecurityResource;
import com.cuc.platform.core.componet.security.service.SecurityResourceService;

/**
 * @author peepul
 *
 * 资源管理控制器
 */
@Controller
@RequestMapping(value = "/security/resc")  
public class SecurityRescController {
	
	/**
	 * @return List<SecurityResource>
	 */
	@RequestMapping(value = "/getRescList",  method = RequestMethod.GET)
	public String getRescList(Model model) {
		List<SecurityResource> list = securityResourceService.getAllRescs();
		model.addAttribute("list", list);

		return "security/resource/rescList"; 
	}
	
	/**
	 * 显示新增页面
	 * 
	 * @return
	 */
	@RequestMapping(value = "/view", method = RequestMethod.GET)
	public String viewAdd(){
		return "security/resource/rescForm";
	}	 
	
	/**
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/edit/{id}", method = RequestMethod.GET)
	public String getResc(@PathVariable("id") Long id, Model model) {
		SecurityResource resc = securityResourceService.getRescById(id);
		
		model.addAttribute("resc", resc);
		return "security/resource/rescForm";
	}
	
	/**
	 * @param model
	 * @return
	 * 
	 * 
	 */
	@RequestMapping(value = "/save",  method = RequestMethod.POST)
	public String save(@ModelAttribute("resc") SecurityResource resc) {
		//如果id为空，则是新增，否则为修改
		Long id = resc.getId();
		if (null != id) {
			securityResourceService.updateResc(resc);//修改
		} else{
			securityResourceService.addResc(resc);//新增
		}
		
		return "redirect:/security/resc/getRescList"; 
	}
	
	@RequestMapping(value = "/del/{id}", method = RequestMethod.GET)
	public String delResc(@PathVariable("id") Long id) {
		securityResourceService.delResc(id);
		
		return "redirect:/security/resc/getRescList";
	}
	
	/**
	 * Ioc
	 */
	@Resource
	private SecurityResourceService securityResourceService;
}
